package com.lujieni.nio.c1;


import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Hello world!
 *
 */
public class TestByteBuffer {
    public static void main( String[] args ) {
        try (FileChannel channel = new FileInputStream("data.txt").getChannel()) {
            // 获取缓冲区
            ByteBuffer allocate = ByteBuffer.allocate(10);
            while (channel.read(allocate) != -1) { //将数据读取缓存
                allocate.flip(); //切换读模式
                while (allocate.hasRemaining()) {
                    byte b = allocate.get();
                    System.out.println((char)b);
                }
                //allocate.clear(); //切换写模式
                allocate.compact();
            }
        } catch (IOException e) {

        }
    }
}
